<template>
  <div>
    <el-main>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </div>
</template>

<script>

import {ElMessage} from "element-plus";

export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        username:'',
        password:''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'change'}
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      console.log("into submit")
      this.$refs[formName].validate((valid) => {
        console.log("into validate")
        if (valid) {
          console.log("is valid")
          console.log(this.axios.defaults.baseURL)
          console.log("=====")
          const _this = this

          _this.axios.post('/admin/login', this.ruleForm).then(res => {
            console.log(res.data)
            const userInfo = res.data.data

            _this.$store.commit("setUserInfo", userInfo)

            console.log(_this.$store.getters.getUser)
            ElMessage.success({
              message: _this.$store.getters.getUser,
              type: 'success'
            })
            _this.$router.push("/main")
          })
        } else {
          console.log("error submit!")
          return false
        }
      })
      console.log("end")
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
